#define _SCL_SECURE_NO_WARNINGS
#include "GameMap.h"

GameMap::GameMap(Gosu::Graphics& gfx, std::vector<bigTile> *bigTiles, std::vector<smallTile> *smallTiles, const int width,const int height) : graphics(&gfx) {



	

	this->width = width;
	this->height = height;

	windowHeight = (AREA_MAX_Y - 40 < this->height * (int)TILESIZE)? (AREA_MAX_Y - 40) / (int)TILESIZE : this->height;
	windowWidth = (AREA_MAX_X - 40 < this->width * (int)TILESIZE)? (AREA_MAX_X - 40) / (int)TILESIZE : this->width;

	displacementX = displacementY = 0;

	bigGrid.resize(boost::extents[width][height]);
	smallGrid.resize(boost::extents[width*2][height*2]);


	for(int i=0;i < width;i++){
		for(int j=0;j < height;j++){

		bigGrid[i][j] = 1;

		}
	}


	for(int i=0;i < width*2;i++){
		for(int j=0;j < height*2;j++){

		smallGrid[i][j] = -1;

		}
	}

	std::wstring filename;

	filename = Gosu::sharedResourcePrefix() + L"arrowUpDown.png";
	arrowUpDown.reset(new Gosu::Image(*graphics, filename, true));

	filename = Gosu::sharedResourcePrefix() + L"arrowLeftRight.png";
	arrowLeftRight.reset(new Gosu::Image(*graphics, filename, true));	

	this->bigTiles = *bigTiles;
	std::vector<bigTile>::iterator it = this->bigTiles.begin();


	while(it != this->bigTiles.end()){

		filename = Gosu::sharedResourcePrefix() + Gosu::widen((*it).path);
		this->bigImages.push_back(new Gosu::Image(*graphics, filename,true));

		it++;

	}


	defineBig(bigTiles->at(0));
	



	this->smallTiles = *smallTiles;
	std::vector<smallTile>::iterator its = this->smallTiles.begin();


	while(its != this->smallTiles.end()){

		filename = Gosu::sharedResourcePrefix() + Gosu::widen((*its).path);
		this->smallImages.push_back(new Gosu::Image(*graphics, filename,true));

		its++;

	}


	defineSmall(smallTiles->at(0));

	isBig = true;
	

}

void GameMap::set(double x, double y){

	if(x > 20 && x < 660 && y > 20 && y < 500){

		int i,j;

		if(isBig){

			i = int((x - 21.0) / TILESIZE);
			j = int((y - 21.0) / TILESIZE);

			bigGrid[i + displacementX][j + displacementY] = currentBig.id;
		}else{

			i = int((x - 21.0) / (TILESIZE * .5));
			j = int((y - 21.0) / (TILESIZE * .5));

			if(currentSmall.id == 0)
				smallGrid[i + displacementX*2][j + displacementY*2] = -1;
			else
				smallGrid[i + displacementX*2][j + displacementY*2] = currentSmall.id;
		}

	}else{

		if(x < 20 && displacementX > 0){

			--displacementX;
			return;
		}

		if(x > 660 && displacementX < width - 16){

			++displacementX;
			return;
		}

		if(y < 20 && displacementY > 0){

			--displacementY;
			return;
		}

		if(y > 500 && displacementY < height - 12){

			++displacementY;
			return;

		}
	}

}

void GameMap::defineBig(bigTile a){

	currentBig = a;

	isBig = true;

}


void GameMap::defineSmall(smallTile a){

	currentSmall = a;

	isBig = false;
	

}

bool GameMap::onMap(double x, double y){


	return (x > AREA_MIN_X && x < AREA_MAX_X &&
			y > AREA_MIN_Y && y < AREA_MAX_Y);


}


void GameMap::draw(){

	arrowUpDown->drawRot(340,10,100,0);
	arrowUpDown->drawRot(340,510,100,180);

	arrowLeftRight->drawRot(10,260,100,0);
	arrowLeftRight->drawRot(670,260,100,180);

	for(int i=0;i < windowWidth;i++){
		for(int j=0;j < windowHeight;j++){

			bigImages.at(bigGrid[i + displacementX][j + displacementY])->draw(i*TILESIZE + 20,j*TILESIZE + 20,1);

		}
	}

	for(int i=0;i < windowWidth * 2;i++){
		for(int j=0;j < windowHeight * 2;j++){

			if(smallGrid[i + displacementX*2][j + displacementY*2] != -1)
				smallImages.at(smallGrid[i + displacementX*2][j + displacementY*2])->draw(i*TILESIZE/2 + 20,j*TILESIZE/2 + 20,(smallTiles.at(smallGrid[i + displacementX*2][j + displacementY*2]).level == 0)? 2 : j+1);

		}
	}
}

void GameMap::loadMap(const char * mapName){


	std::ifstream  myfile (mapName);
	if (myfile.is_open()){

		myfile >> width;
		myfile >> height;

		bigGrid.resize(boost::extents[width][height]);
		smallGrid.resize(boost::extents[width*2][height*2]);


		for(int i=0;i < height;i++){
			for(int j=0;j < width;j++){

			myfile >> bigGrid[j][i];

			}
		}

		for(int i=0;i < height*2;i++){
			for(int j=0;j < width*2;j++){

			myfile >> smallGrid[j][i];

			}
		}
		
	}

	myfile.close();


}

void GameMap::saveMap(const char * mapName){


	std::ofstream myfile (mapName);

	

	if (myfile.is_open()){

		myfile << width << " ";
		myfile << height << " ";

		for(int i=0;i < height;i++){
			for(int j=0;j < width;j++){

			myfile << bigGrid[j][i] << " ";

			}
		}


		for(int i=0;i < height*2;i++){
			for(int j=0;j < width*2;j++){

			myfile << smallGrid[j][i] << " ";

			}
		}
		
		myfile.close();

	}
}